#include "QControlUtils.h"
#include "qwt_slider.h"
#include <QLineEdit>

void SetManualValue(QwtKnob* pKnob, double value)
{
    pKnob->blockSignals(true);
    pKnob->setValue(value);
    pKnob->blockSignals(false);
}

void SetManualValue(QComboBox* pCombo, int value)
{
    pCombo->blockSignals(true);
    pCombo->setCurrentIndex(value);
    pCombo->blockSignals(false);
}

void SetManualValue(QDoubleSpinBox* pSpin, double value)
{
    QObject* pParent = pSpin->parent();
    if (pParent != NULL && pSpin->objectName().startsWith("doubleSpinBox"))
    {
        QString name(pSpin->objectName().mid(14));
        QwtSlider* pSlider = pParent->findChild< QwtSlider* >("Slider_" + name);
        if (pSlider != NULL)
        {
            pSlider->blockSignals(true);
            pSlider->setValue(value);
            pSlider->blockSignals(false);
        }

        QLineEdit* pLineEdit = pParent->findChild< QLineEdit* >("lineEdit_" + name);
        if (pLineEdit != NULL)
        {
            pLineEdit->setText(QString::number(value, 'f', 1));
        }

    }
    pSpin->blockSignals(true);
    pSpin->setValue(value);
    pSpin->blockSignals(false);
}

void SetManualValue(QComboBox* pCombo, QVariant userData)
{

    int  idx = pCombo->findData(userData);
    if (idx >= 0)
        SetManualValue(pCombo, idx);
}

void SetManualValue(QCheckBox* checkbox, bool checked)
{
    checkbox->blockSignals(true);
    checkbox->setChecked(checked);
    checkbox->blockSignals(false);
}


static const RadarRange g_ranges_metric[] = {
    { 160, 91, "160 m", "25", "50", "75"  ,4, 40 , "40 m"},
    { 250, 139, "250 m", 0, "125", 0  ,5, 50 , "50 m"},
    { 500, 278, "500 m", "125", "250", "375" ,5,100, "100 m" },
    { 750, 417, "750 m", 0, "375", 0  ,5 , 150 ,"150 m" },
    { 1000, 556, "1 km", "250", "500", "750"  ,5 , 200 , "200m "},
    { 1500, 833, "1.5 km", 0, "750", 0 , 6, 250 ,"1/4 Km" },
    { 3000, 1666, "3 km", "500", "1000", "1500" , 6 , 500 , "1/2 Km" },
    { 6000, 3333, "6 km", 0, "1500", 0 , 6,1000 , "1 Km"},
    { 8000, 4444, "8 km", 0, "1500", 0  ,8 ,1000 , "1 Km" },
    { 12000, 6666, "12 km", "3", "6", "9" , 6,2000 , "2 km" },
    { 16000, 8889, "16 km", "4", "8", "12" , 8 ,2000 , "2 Km" },
    { 24000, 13333, "24 km", "6", "12", "18" , 6 , 4000 , "4 Km" },
    { 36000, 20000, "36 km", "9", "18", "27" , 6 , 6000 ,"6 Km" },
    { 48000, 26667, "48 km", "12", "24", "36" ,6, 8000 , "8 Km" },
    { 64000, 35556, "64 km", "9", "18", "27" , 8 ,8000 , "8Km" },
    { 72000, 40000, "72 km", "9", "18", "27" , 8 ,9000 , "9 Km"},
    { 96000, 53334, "96 km", "9", "18", "27", 8 ,12000 ,"12 Km"},
    { 128000, 71111, "128 km", "9", "18", "27" , 8 ,16000 ,"16 Km"}
};

static const RadarRange g_ranges_nautic[] = {
    { 50, 98, "50 m", 0, 0, 0 },
{ 75, 146, "75 m", 0, 0, 0 },
{ 100, 195, "100 m", "25", "50", "75" },
{ 1852 / 8, 451, "1/8 NM", 0, "1/16", 0 },
{ 1852 / 4, 673, "1/4 NM", "1/32", "1/8", 0 },
{ 1852 / 2, 1389, "1/2 NM", "1/8", "1/4", "3/8" },
{ 1852 * 3 / 4, 2020, "3/4 NM", 0, "3/8", 0 },
{ 1852 * 1, 2693, "1 NM", "1/4", "1/2", "3/4" },
{ 1852 * 3 / 2, 4039, "1.5 NM", "3/8", "3/4", 0 },
{ 1852 * 2, 5655, "2 NM", "0.5", "1.0", "1.5" },
{ 1852 * 3, 8079, "3 NM", "0.75", "1.5", "2.25" },
{ 1852 * 4, 10752, "4 NM", "1", "2", "3" },
{ 1852 * 6, 16128, "6 NM", "1.5", "3", "4.5" },
{ 1852 * 8, 22208, "8 NM", "2", "4", "6" },
{ 1852 * 12, 36352, "12 NM", "3", "6", "9" },
{ 1852 * 16, 44416, "16 NM", "4", "8", "12" },
{ 1852 * 24, 72704, "24 NM", "6", "12", "18" },
{ 1852 * 36, 72704, "36 NM", "9", "18", "27" } };

const RadarRange* convertMetersToRadarAllowedValue(int setup_m, int& idx)
{
    int max_r = ARRAY_SIZE(g_ranges_metric) - 1;

    for (; max_r >= 0; max_r--)
    {
        if (setup_m >= g_ranges_metric[max_r].actual_meters)
            break;
    }

    idx = max_r;
    return &g_ranges_metric[max_r];

    /*for (int i = 0; i < max_r; i++)
    {
        if (g_ranges_metric[i].actual_meters == setup_m)
            return &g_ranges_metric[i];
    }
    return NULL;*/
}


int getRadarRangeSize(int uints)
{
    return 	(uints == 0) ? ARRAY_SIZE(g_ranges_metric) : ARRAY_SIZE(g_ranges_nautic);
}

const RadarRange* getRadarRangeArray(int uints)
{
    return (uints == 0) ? g_ranges_metric : g_ranges_nautic;
}
static QString Slot(const QString& slot)
{
    return QString::number(QSLOT_CODE) + slot;
}

void connectDefaultSlot(QWidget* pParent, QObject* manager)
{
    QList< QDoubleSpinBox* > spins(pParent->findChildren< QDoubleSpinBox* >(QRegExp("^doubleSpinBox")));
    foreach(QDoubleSpinBox* pSpin, spins)
    {
        QString name(pSpin->objectName().mid(14));
        QObject::connect(pSpin, SIGNAL(valueChanged(double)), manager, Slot(name + "_valueChanged(double)").toLatin1().data());
        QwtSlider* pSlider = pParent->findChild< QwtSlider* >("Slider_" + name);

        if (pSlider != NULL)
        {
            QObject::connect(pSlider, SIGNAL(sliderMoved(double)), pSpin, SLOT(setValue(double)));
            QObject::connect(pSpin, SIGNAL(valueChanged(double)), pSlider, SLOT(setValue(double)));
        }
    }
}

void SetWidgetsEnable(QWidget* parent, const QRegExp &regExp, bool enable)
{
    QList< QWidget* > widgets(parent->findChildren< QWidget* >(regExp));
    foreach(QWidget* widget, widgets)
    {
        widget->setEnabled(enable);
    }
}

double ToDegreesDouble(double degrees)
{
    while (degrees < 0) degrees += 360.0;
    return degrees;
}